package net.mcg_server.jbi.gui;

import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import net.mcg_server.jbi.Program;
import net.mcg_server.jbi.data.Game;
import net.mcg_server.jbi.gui.menu.MenuBar;

public class EditGame {

	// -------------
	// VARIABLES
	// -------------
	private JFrame frame;
	private JPanel panel;
	private Game game;
	private JTextArea descriptionField;
	private JCheckBox isBeaten;
	private JComboBox<String> gameList;

	public EditGame(Program program) {
		initialize(program);
		frame.setVisible(true);
	}

	private void initialize(final Program program) {
		// create the frame
		frame = new JFrame("Edit A Game");
		frame.setBounds(250, 200, 400, 300);
		frame.toFront();
		frame.setResizable(false);

		// create the panel
		panel = new JPanel();
		panel.setLayout(null);

		frame.add(panel);

		//List label
		JLabel listLabel = new JLabel("Select a game to modify:");
		panel.add(listLabel);

		//Get array of game titles
		gameList = new JComboBox<String>();
		gameList.addItem("");
		ArrayList<Game> games = program.getGameList();
		for(int i = 0; i < games.size(); i++){
			game = program.getGameList().get(i);
			gameList.addItem(game.getTitle());
		}

		//Create JList
		panel.add(gameList);

		//Create Description label + field
		JLabel descriptionLabel = new JLabel("Description:");
		descriptionField = new JTextArea();
		panel.add(descriptionLabel);
		panel.add(descriptionField);

		//Create isBeaten label & CheckBox
		JLabel isBeatenLabel = new JLabel("Is Beaten?");
		isBeaten = new JCheckBox();
		panel.add(isBeatenLabel);
		panel.add(isBeaten);

		//Add Game button
		JButton editButton = new JButton("Edit Game");
		panel.add(editButton);

		// Set layout positioning
		Insets insets = panel.getInsets();
		Dimension size = listLabel.getPreferredSize();
		listLabel.setBounds(30+insets.left, 20+insets.top, size.width, size.height);

		size = gameList.getPreferredSize();
		gameList.setBounds(30+insets.left, 40+insets.top, 340, size.height);

		size = descriptionLabel.getPreferredSize();
		descriptionLabel.setBounds(30+insets.left, 80+insets.top, size.width, size.height);

		descriptionField.setBounds(120+insets.left, 80+insets.top, 250, 50);

		size = isBeatenLabel.getPreferredSize();
		isBeatenLabel.setBounds(30+insets.left, 150+insets.top, size.width, size.height);

		size = isBeaten.getPreferredSize();
		isBeaten.setBounds(120+insets.left, 150+insets.top, size.width, size.height);

		size = editButton.getPreferredSize();
		editButton.setBounds(120+insets.left, 180+insets.top, 160, size.height);


		//------------
		// LISTENERS
		//------------

		// fill in description and checkbox upon selecting a game
		gameList.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Boolean beatenStatus = false;

				if(e.getActionCommand().equals("comboBoxChanged")){
					int index = 0;
					for(int i=0; i < program.getGameList().size(); i++){
						if(program.getGameList().get(i).getTitle().equals((String) gameList.getSelectedItem())){
							index = i;
							beatenStatus = program.getGameList().get(i).isBeaten();
						}
					}
					descriptionField.setText(program.getGameList().get(index).getDescription());
					isBeaten.setSelected(beatenStatus);
				}
			}
		});

		// Edit button action
		editButton.addMouseListener(new MouseListener(){
			@Override
			public void mouseClicked(MouseEvent e){

				if(true){
					Game game = null;
					int index = 0;
					for(int i = 0; i < program.getGameList().size(); i++){
						if(((String) gameList.getSelectedItem()).equals(program.getGameList().get(i).getTitle())){
							game = program.getGameList().get(i);
							index = i;
						}
					}
					game.setDescription(descriptionField.getText());
					game.setBeaten(isBeaten.isSelected());
					program.getGameList().set(index, game);
					program.getMainPanel().loadData();

					MenuBar.menuOpened = false;
					program.getMainFrame().requestFocus();
					frame.dispose();
					
				}
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
			}
		});

		// close AccountManageWindow
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				if (true) {
					MenuBar.menuOpened = false;
					program.getMainFrame().requestFocus();
					frame.dispose();
				}
			}
		});

		frame.addWindowFocusListener(new WindowAdapter(){
			@Override
			public void windowLostFocus(WindowEvent e){
				frame.requestFocus();
			}
		});
	}

}
